import time


class StopWatch:
    def __init__(self):
        self.start_time = time.time()

    def elapsed_time(self, unit: str = 's', precision: int = 3) -> float:
        """
        返回程序所用时间

        :param unit : 单位, s(秒), m(分), h(时)
        :param precision : 小数点后保留几位

        :returns time : 程序所用时间
        """
        if unit == 's':
            return f'{round(time.time() - self.start_time, precision)} s'
        elif unit =='m':
            return f'{round((time.time() - self.start_time) / 60, precision)} m'
        elif unit == 'h':
            return f'{round((time.time() - self.start_time) / 3600, precision)} h'
        else:
           ValueError('Invalid unit')


if __name__ == '__main__':

    stopwatch = StopWatch()
    time.sleep(60)
    print(stopwatch.elapsed_time(unit='s', precision=5))  # 1.0001
    print(stopwatch.elapsed_time(unit='m', precision=5))  # 0.02
    print(stopwatch.elapsed_time(unit='h', precision=5))  # 0.00
